package com.auth.common.result;

import lombok.Data;

/**
 * @Author:Tangjiachang
 * @Date 2023/7/12
 * @Description:
 */
@Data
public class Result<DATA> {
    private Integer code;
    private String message;
    private DATA data;

    private Result() { }


    public static<DATA> Result<DATA> build(DATA body, ResultCodeEnum resultCodeEnum) {
        Result<DATA> result = new Result<>();
        result.setData(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static<DATA> Result<DATA> ok() {
        return build(null, ResultCodeEnum.SUCCESS);
    }

    public static<DATA> Result<DATA> ok(DATA data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static<DATA> Result<DATA> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }

    public static<DATA> Result<DATA> fail(DATA data) {
        return build(data, ResultCodeEnum.FAIL);
    }

    public Result<DATA> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result<DATA> setMessage(String message) {
        this.message = message;
        return this;
    }
}
